package [path_1].[path_2].common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import [path_1].[path_2].model.common.dtos.ResponseResult;
import [path_1].[path_2].model.common.enums.AppHttpCodeEnum;

@RestControllerAdvice//控制器增强
@Slf4j
@Configuration
public class ExceptionCatch {


  //捕获Exception此类异常
  @ExceptionHandler(Exception.class)
  public ResponseResult exception(Exception exception) {
    exception.printStackTrace();
    //记录日志
    log.error("catch exception:{}", exception.getMessage());
    //返回通用异常
    return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
  }

  //可预知异常
  @ExceptionHandler(CustomException.class)
  public ResponseResult exception(CustomException e){
    e.printStackTrace();
    log.error("catch CustomException:{}",e.getMessage());
    return ResponseResult.errorResult(e.getAppHttpCodeEnum());
  }
}
